import {Component, OnDestroy, OnInit, ViewEncapsulation} from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {Subscription} from "rxjs/Subscription";
import {ToolsService} from "@shared/service/tools/tools.service";
import {HomepageService} from "@homepage/homepage.service";
import {AuthService} from "@shared/service/auth/auth.service";
import {ToolbarService} from "@shared/components/toolbar/toolbar.service";

@Component({
  selector: 'seed-my-care-fans',
  templateUrl: './follows.component.html',
  styleUrls: ['./follows.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class FollowsComponent implements OnInit, OnDestroy {
  public titleName: string;
  public myCareFans: any = [];
  scrollContainer;

  subscription: Subscription = new Subscription();

  constructor(public router: Router,
              public toolsService: ToolsService,
              public homepageService: HomepageService,
              public _activatedRoute: ActivatedRoute,
              public user: AuthService,
              public toolBarService: ToolbarService) {
    this.myCareFans['list'] = [];
  }

  dealData(resData) {
    if (resData.result === '0' && resData.data.list.length > 0) {
      this.myCareFans['list'] = resData.data.list;
    } else {
      this.myCareFans['list'] = '无';
    }
    this.toolsService.hideLoading();
  }

  setTitle(userId) {
    (this.user.getUserId() && this.user.getUserId() === userId)
      ? this.titleName = '我的关注'
      : this.titleName = '他的关注';
    this.toolsService.setTitle(this.titleName);
  }

  gotoHomepage(itemUserid) {
    this.router.navigate(['/', itemUserid]);
  }

  ngOnInit() {
    this.toolsService.showLoading();
    this.subscription.add(
      this._activatedRoute.queryParams.do(() => this.toolBarService.unfoldNav())
        .subscribe(param => {
          this.homepageService.getMycare(param['userId']).subscribe(res => {
            this.dealData(res);
          });
          this.setTitle(param['userId']);
        })
    );
  }

  ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}
